<?php

namespace app\logic\model\music;

use app\logic\model\LogicModel;
use app\logic\single\GoodsSingle;
use think\model\concern\SoftDelete;

class MusicWelfareLogicModel extends LogicModel
{
    use SoftDelete;
    protected $name='music_welfare';


    public function getList(\think\Request $request, array $where, string $field='*', $page = 1, $limit=15,$order='id desc')
    {
        $query=$this->where($where)
            ->where(function ($q) use ($request){
                if(check_request($request,'title')){
                    $q->whereLike("title","%{$request['title']}%");
                }
                if(check_request($request,'user_id')){
                    $q->where("user_id",$request['user_id']);
                }
                if(check_request($request,'start_time')&&check_request($request,'end_time')){
                    $q->whereBetween("create_time",[$request['start_time'],$request['end_time']." 23:59:59"]);
                }elseif (check_request($request,'start_time')){
                    $q->where("create_time",">=",$request['start_time']);
                }elseif (check_request($request,'end_time')){
                    $q->where("create_time","<=",$request['end_time']." 23:59:59");
                }
            });
        $list = $query->field($field)->order($order)->page($page,$limit)->select();
        $count = $query->count();
        if($count >= 1){
            $list = $list->toArray();
            foreach ($list as &$val){
                $val['goods'] = GoodsSingle::getInstance()->getGoodsByType($val['goods_id'],$val['type']);
            }
            unset($val);
        }
        return [$list,$count];
    }
}